/**
 * 
 */
package com.local.fastJson.fastJsonDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;

/**

 * @author admin
 * fastjson 生成器、解析器
 * 生成器: JSONArray.toJSONString(...);
 * fastjson生成json字符串(JavaBean,List<JavaBean>,List<String>,List<Map<String,Object>)
 * 解析器:
 * JavaBean用 JSON.parseObject(str, JavaBean.class)生成；//JavaBean.class代表你要解析的dto
 * List<JavaBean>用JSON.parseArray(str, JavaBean.class); //JavaBean.class代表你要解析的dto
 * List<String>用JSON.parseArray(str, String.class); 
 * List<Map<String,Object>使用的是JSON.parseObject(str, new TypeReference<List<Map<String,Object>>>(){});
 */


class FastJson_class {
	
	
	public List<HashMap<String,Object>> getFastJson(){
		List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
		for(int i = 0; i < 3; i++) {
			HashMap<String,Object> hash = new HashMap<String,Object>();
			switch(i) {
			case 0:
				hash.put(i+"", "阿雪");
				//hash.put(i+"", i);
				break;
			case 1:
				hash.put(i+"", "小静");
				//hash.put(i+"", i);
				break;
			case 2:
				hash.put(i+"", "小月");
				//hash.put(i+"", i);
				break;
			}
			list.add(hash);	
		}
		return list;
	}
	
	
	public List<String> FastJsonString(){
		List<String> list = new ArrayList<String>();
		for(int i = 0; i < 3; i++) {
			switch(i) {
			case 0:
				list.add("阿雪");	
				break;
			case 1:
				list.add("小静");	
				break;
			case 2:
				list.add("小月");	
				break;
			}		
		}
		return list;
	}
	
}



public class FastJsonDemo {
	
	private final static Logger log = LoggerFactory.getLogger(FastJson_class.class); 
	
	public void reflectList() {

		//Logger log = Logger.getLogger(FastJsonDemo.class);  


		

		FastJson_class fc = new FastJson_class();
		String str = JSONArray.toJSONString(fc.getFastJson());
		log.info("list_hashmap转String："+str);
		
		List<HashMap<String, Object>> listMap = JSON.parseObject(str, new TypeReference<List<HashMap<String,Object>>>(){});  
		log.info("String转list_hashmap："+listMap);
		
		
		String str1 = JSONArray.toJSONString(fc.FastJsonString());
		log.info("list_string转String："+str1);
		List<String> listString = JSON.parseArray(str1, String.class);
		log.info("原list_string："+fc.FastJsonString());
		log.info("String转list_string："+listString);
	}
	/**

	 * @param args

	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub


           new FastJsonDemo().reflectList();
           
           //dPropertyConfigurator.configure( " D:/workspace/gan/Java_test/log4j.properties " );  


          // Logger logger = Logger.getLogger(FastJsonDemo.class);  


          // logger.debug( " debug " );  


           //logger.error( " error " );  


           //logger.info("");


	}
}
